package com.tbit.uqbike.client.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.tbit.uqbike.client.constant.BrandConstant;
import com.tbit.uqbike.client.dao.core.AccountDao;
import com.tbit.uqbike.client.pojo.AccountConfig;
import com.tbit.uqbike.client.pojo.Result;
import com.tbit.uqbike.client.pojo.vo.OutAgeConfig;
import com.tbit.uqbike.client.service.AccountConfigService;
import com.tbit.uqbike.client.service.CacheService;
import com.tbit.uqbike.client.service.OutAgeService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.Objects;

/**
 * 一键停运公告
 * @author <a href="mailto:liu.haoxiang@tbit.com.cn">BUGXiang</a>
 * @version 1.0
 * @since 2025-2-5
 */
@Service
public class OutAgeServiceImpl implements OutAgeService {


    @Resource
    private CacheService cacheService;
    @Resource
    private AccountConfigService accountConfigService;
    @Resource
    private AccountDao accountDao;


    /**
     * 获取停运公告配置
     * @param accountId 区域id
     * @return 停运公告配置
     */
    @Override
    public Result<OutAgeConfig> getOutAge(Integer accountId) {
        //判断品牌是否停运 优先使用品牌停运公告
        Date date = new Date();
        Integer brandId = accountDao.getBrandId(accountId);

        JSONObject brandJsonConfig = cacheService.getBrandJsonConfig(brandId, BrandConstant.SERVICE_SWITCH);
        if (Objects.nonNull(brandJsonConfig.getString("serviceSwitch")) && brandJsonConfig.getString("serviceSwitch").equals("0")){
            return Result.success(checkIsOutAge(brandId, date, 0));
        }
        AccountConfig accountConfig = accountConfigService.getByAccountId(accountId);
        if (accountConfig == null || !accountConfig.getServiceSwitch()) {
            return Result.success(checkIsOutAge(accountId, date, 1));
        }
        return Result.success();
    }


    /**
     * 判断当前时间是否在停运公告时间
     * @param brandId 品牌id
     * @param date 当前时间
     * @param type 类型 0品牌 1区域
     * @return OutAgeConfig
     */
    private OutAgeConfig checkIsOutAge(Integer brandId, Date date, Integer type) {
        OutAgeConfig outAgeConfig;
        if (0 == type) {
            outAgeConfig = cacheService.getOutAgeConfig(brandId, null);
        } else {
            outAgeConfig = cacheService.getOutAgeConfig(null, brandId);
        }

        if (Objects.isNull(outAgeConfig)) {
            return null;
        }
        //判断当前时间是否在公告展示时间内
        Date startTime = outAgeConfig.getAnnouncementStartTime();
        Date endTime = outAgeConfig.getAnnouncementEndTime();
        if (Objects.nonNull(startTime) && Objects.nonNull(endTime) && !isTimeWithInRange(startTime, endTime, date)) {
            return null;
        }
        return outAgeConfig;
   }


    /**
     * 判断当前时间是否在时间范围内
     * @param startTime 开始时间
     * @param endTime 结束时间
     * @param currentTime 当前时间
     * @return boolean
     */
    private boolean isTimeWithInRange(Date startTime, Date endTime, Date currentTime){
        // 判断当前时间是否在开始时间和结束时间之间
        return !currentTime.before(startTime) && !currentTime.after(endTime);
    }
}
